R Coding Workshop: 4th Meeting

GIS & Geospatial Data Analysis (Fall 2025)

R Coding Workshop: 4th Meeting

Outline

  • R Functions
  • dplyr functions
    • arrange()
    • slice_sample()
  • define your own function
  • ggplot2

R Functions

library(tidyverse)
library(sf)
library(mapview)
library(rnaturalearth)
library(viridis)

Using base and imported functions

Functions from Base R

class(print) |> print()
[1] "function"
class(class)
[1] "function"
long_df <- tibble(
    value = 1:37
)
print(long_df)
# A tibble: 37 × 1
   value
   <int>
 1     1
 2     2
 3     3
 4     4
 5     5
 6     6
 7     7
 8     8
 9     9
10    10
# ℹ 27 more rows
long_df |> print(n = 37)
# A tibble: 37 × 1
   value
   <int>
 1     1
 2     2
 3     3
 4     4
 5     5
 6     6
 7     7
 8     8
 9     9
10    10
11    11
12    12
13    13
14    14
15    15
16    16
17    17
18    18
19    19
20    20
21    21
22    22
23    23
24    24
25    25
26    26
27    27
28    28
29    29
30    30
31    31
32    32
33    33
34    34
35    35
36    36
37    37

Imported functions

class(select)
[1] "function"
class(mapview)
[1] "standardGeneric"
attr(,"package")
[1] "methods"

Components of a Function

When we want to write our own Function

  • copy-and-paste
  • vector operations
  • creating plots

Components of a Function

  • Function Name: Usually a verb
  • Parameters: Variable provided when you defined your function,they act as a placeholder for expected input
  • Arguments: Variable specified at the time you use the function
  • Return: Output of the function
add_four <- function(x) x + 4
add_four |> class() |> print()
[1] "function"
add_four <- function(x) x + 4
add_four |> class() |> print()
[1] "function"
a <- 1.46
add_four(a)
[1] 5.46
v1 <- c(1, 9, 8, 8)
add_four(v1)
[1]  5 13 12 12
v1 + 4
[1]  5 13 12 12
sapply(v1, add_four)
[1]  5 13 12 12
long_df$value |> add_four()
 [1]  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
[26] 30 31 32 33 34 35 36 37 38 39 40 41
book_challenge_sf <- st_read('data/book-challenges-state-sf.gpkg')
Reading layer `book-challenges-state-sf' from data source 
  `/Users/toyuan/dohnanyi/data/book-challenges-state-sf.gpkg' 
  using driver `GPKG'
Simple feature collection with 51 features and 9 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -179.1435 ymin: 18.90612 xmax: 179.7809 ymax: 71.4125
Geodetic CRS:  WGS 84
book_challenge_sf |> mapview(zcol = 'count')

Getting familiar with how functions work

  • paste() and paste0() function
create_label <- function(unit, count) {
    paste0(unit, ': ', count)
}
book_challenge_sf <- book_challenge_sf |> mutate(lab = create_label(gn_name, count))
book_challenge_sf |> mapview(zcol = 'count', label = 'lab')

Plotting Function

book_challenge_sf |>
  filter(!(postal %in% c('HI', 'AK'))) |>
  ggplot() +
  geom_sf(
    aes(fill = count),
    color = 'white',
    size = 0.1
  ) +
  geom_sf_label(
    aes(label = postal),
    size = 2.5,
    fill = 'white',
    alpha = 0.7,
    color = 'black'
  ) +
  scale_fill_viridis(na.value = '#E2E2E2') +
  theme_minimal() +
  labs(
    title = 'Book Challenges by State',
    subtitle = '2000-2010'
  )

plot_state_choropleth <- function(sf, col_str, plt_title) {
    sf |>
    filter(!(postal %in% c('HI', 'AK'))) |>
    ggplot() +
    geom_sf(
      aes_string(fill = col_str),
      color = 'white',
      size = 0.1
    ) +
    geom_sf_label(
      aes(label = postal),
      size = 2.5,
      fill = 'white',
      alpha = 0.7,
      color = 'black'
    ) +
    scale_fill_viridis(na.value = '#E2E2E2') +
    theme_minimal() +
    labs(
      title = plt_title,
      subtitle = '2000-2010'
    )
}
plot_state_choropleth(
    book_challenge_sf,
    'count',
    'Book Challenges by State'
    )

plot_state_choropleth(book_challenge_sf, 'median_income', 'State Median Income')

book_challenge_sf |> plot_state_choropleth('political_value_index', 'Political Value Index')

arrange()

  • Sort observations by column value
book_challenge_sf |>
  arrange(desc(count)) |>
  pull(lab)
 [1] "Pennsylvania: 147"        "Oregon: 118"             
 [3] "Colorado: 80"             "California: 50"          
 [5] "Illinois: 39"             "Michigan: 34"            
 [7] "Virginia: 33"             "Ohio: 29"                
 [9] "Florida: 26"              "New York: 24"            
[11] "Oklahoma: 23"             "North Carolina: 20"      
[13] "Indiana: 20"              "Minnesota: 17"           
[15] "South Carolina: 15"       "Idaho: 14"               
[17] "Vermont: 13"              "Georgia: 13"             
[19] "Kansas: 13"               "Iowa: 13"                
[21] "Kentucky: 13"             "Tennessee: 13"           
[23] "Arizona: 12"              "Louisiana: 12"           
[25] "New Jersey: 11"           "Missouri: 11"            
[27] "Alabama: 10"              "Wisconsin: 10"           
[29] "Washington: 9"            "Alaska: 9"               
[31] "Massachusetts: 8"         "Montana: 7"              
[33] "New Hampshire: 7"         "North Dakota: 6"         
[35] "Connecticut: 6"           "Maryland: 5"             
[37] "Arkansas: 5"              "Wyoming: 4"              
[39] "Maine: 3"                 "New Mexico: 3"           
[41] "Rhode Island: 3"          "South Dakota: 3"         
[43] "West Virginia: 3"         "Delaware: 2"             
[45] "Nebraska: 2"              "Mississippi: 1"          
[47] "Utah: 1"                  "Texas: NA"               
[49] "District of Columbia: NA" "Hawaii: NA"              
[51] "Nevada: NA"              
book_challenge_sf |>
  arrange(political_value_index) |>
  select(gn_name, count, political_value_index)
gn_name count political_value_index geom
Utah 1 -20.2 MULTIPOLYGON (((-111.0502 4…
Wyoming 4 -19.7 MULTIPOLYGON (((-109.0463 4…
Idaho 14 -17.4 MULTIPOLYGON (((-117.0382 4…
Oklahoma 23 -16.9 MULTIPOLYGON (((-103.0002 3…
Nebraska 2 -13.5 MULTIPOLYGON (((-104.0537 4…
Alaska 9 -13.4 MULTIPOLYGON (((-141.0056 6…
Alabama 10 -13.2 MULTIPOLYGON (((-87.41958 3…
Kansas 13 -11.5 MULTIPOLYGON (((-102.0396 3…
North Dakota 6 -10.4 MULTIPOLYGON (((-104.0476 4…
Kentucky 13 -10.4 MULTIPOLYGON (((-89.42446 3…
Louisiana 12 -9.7 MULTIPOLYGON (((-89.52599 3…
Mississippi 1 -9.5 MULTIPOLYGON (((-88.40221 3…
South Dakota 3 -8.9 MULTIPOLYGON (((-104.0567 4…
Arkansas 5 -8.8 MULTIPOLYGON (((-90.30422 3…
Tennessee 13 -8.7 MULTIPOLYGON (((-90.30422 3…
West Virginia 3 -7.9 MULTIPOLYGON (((-82.58945 3…
South Carolina 15 -7.8 MULTIPOLYGON (((-78.57316 3…
Montana 7 -7.1 MULTIPOLYGON (((-116.0482 4…
Georgia 13 -6.8 MULTIPOLYGON (((-80.89029 3…
Indiana 20 -6.2 MULTIPOLYGON (((-84.80608 4…
Arizona 12 -6.1 MULTIPOLYGON (((-111.0063 3…
North Carolina 20 -4.3 MULTIPOLYGON (((-76.03173 3…
Missouri 11 -3.1 MULTIPOLYGON (((-95.31725 4…
Florida 26 -1.8 MULTIPOLYGON (((-87.44734 3…
Virginia 33 -1.7 MULTIPOLYGON (((-76.01325 3…
Ohio 29 -0.7 MULTIPOLYGON (((-80.52023 4…
Colorado 80 -0.2 MULTIPOLYGON (((-109.0463 4…
Iowa 13 1.0 MULTIPOLYGON (((-96.48266 4…
New Hampshire 7 1.6 MULTIPOLYGON (((-71.50585 4…
Pennsylvania 147 2.0 MULTIPOLYGON (((-79.76301 4…
Minnesota 17 2.3 MULTIPOLYGON (((-97.22609 4…
New Mexico 3 2.4 MULTIPOLYGON (((-108.1375 3…
Wisconsin 10 2.4 MULTIPOLYGON (((-87.80425 4…
Michigan 34 3.8 MULTIPOLYGON (((-84.4913 46…
Oregon 118 4.0 MULTIPOLYGON (((-124.4924 4…
New Jersey 11 4.4 MULTIPOLYGON (((-75.54133 3…
Washington 9 5.0 MULTIPOLYGON (((-122.753 48…
Maine 3 5.5 MULTIPOLYGON (((-71.08495 4…
Delaware 2 7.0 MULTIPOLYGON (((-75.05809 3…
Connecticut 6 7.1 MULTIPOLYGON (((-73.6417 41…
California 50 7.4 MULTIPOLYGON (((-114.7243 3…
Illinois 39 7.7 MULTIPOLYGON (((-89.1237 36…
Maryland 5 8.5 MULTIPOLYGON (((-75.64786 3…
New York 24 10.2 MULTIPOLYGON (((-79.06523 4…
Rhode Island 3 11.2 MULTIPOLYGON (((-71.23686 4…
Massachusetts 8 11.7 MULTIPOLYGON (((-71.19396 4…
Vermont 13 13.4 MULTIPOLYGON (((-73.35134 4…
Texas NA NA MULTIPOLYGON (((-103.3115 2…
District of Columbia NA NA MULTIPOLYGON (((-77.02293 3…
Hawaii NA NA MULTIPOLYGON (((-154.8996 1…
Nevada NA NA MULTIPOLYGON (((-114.0425 4…
book_challenge_df <- read_csv('data/book-challenge11.csv')
book_challenge_df |> arrange(author, title) |> head(10)
title book_id author date year removed explicit antifamily occult language lgbtq violent state political_value_index median_income hs_grad_rate college_grad_rate
Go and Come Back 721 Abelove, Joan 2000-06-18 2000 0 1 0 0 0 0 0 WV -7.9 -7290.0 -4.361958 -9.2237299
Finding Our Way–The Teen Girl Survival Guide 695 Abner, Allison 2002-05-21 2002 0 1 0 0 0 0 0 AZ -6.1 3490.5 1.438042 -0.5237299
Death Blossoms 522 Abu-Jamal, Mumia 2005-07-03 2005 0 0 0 0 0 0 0 OR 4.0 1274.5 5.538042 1.0762701
Tijuana Bibles 1811 Adelman, Bob 2000-06-29 2000 0 1 0 0 0 0 1 OR 4.0 1274.5 5.538042 1.0762701
Art for Lovers 160 Adler, Sabine 2005-04-08 2005 0 0 0 0 0 0 0 OR 4.0 1274.5 5.538042 1.0762701
Psychic Academy Series 1484 Aki, Katsu 2008-06-07 2008 0 0 0 0 0 0 0 VA -1.7 11296.0 1.938042 5.4762701
Monkey High Vol. 3 1277 Akira, Shouko 2009-05-06 2009 0 0 0 0 0 0 0 MI 3.8 2966.0 3.838042 -2.2237299
Absolutely True Diary of a Part-Time Indian, The 69 Alexie, Sherman 2008-10-02 2008 1 0 0 0 0 0 0 IA 1.0 3921.5 6.538042 -2.8237299
Absolutely True Diary of a Part-Time Indian, The 69 Alexie, Sherman 2009-05-04 2009 0 1 0 0 1 0 0 IL 7.7 6489.5 1.838042 2.0762701
Absolutely True Diary of a Part-Time Indian, The 69 Alexie, Sherman 2010-05-12 2010 1 0 0 0 1 0 1 MO -3.1 1279.5 1.738042 -2.4237299

slice_sample()

book_challenge_df |> slice_sample(n = 10)
title book_id author date year removed explicit antifamily occult language lgbtq violent state political_value_index median_income hs_grad_rate college_grad_rate
Green Bible 753 NA 2010-05-26 2010 0 0 0 0 0 0 0 NC -4.3 -310.0 -1.461958 -1.52373
Pleasure 1458 Dickey, Eric Jerome 2009-03-24 2009 0 1 0 0 0 0 0 MI 3.8 2966.0 3.838042 -2.22373
Earth, My Butt and Other Big Round Things, The 607 Mackler, Carolyn 2009-11-09 2009 0 0 0 0 0 0 0 PA 2.0 4218.0 2.338042 -1.62373
Grendel 756 Gardner, John 2001-04-25 2001 0 0 0 0 0 0 0 TN -8.7 -2995.5 -3.661958 -4.42373
Jason & Kyra 1036 Davidson, Dana 2009-11-09 2009 0 0 0 0 0 0 0 PA 2.0 4218.0 2.338042 -1.62373
Murder Mysteries 990 Gaiman, Neil and P. Craig Russell 2008-01-15 2008 0 1 0 0 0 0 0 OH -0.7 2469.0 3.438042 -2.92373
Key of Valor 1071 Roberts, Nora 2005-10-01 2005 0 0 0 0 0 0 0 OR 4.0 1274.5 5.538042 1.07627
Coldest Winter Ever, The 419 Souljah, Sister 2005-09-24 2005 0 1 0 0 1 0 0 PA 2.0 4218.0 2.338042 -1.62373
And Tango Makes Three 143 Parnell, Peter and Justin Richardson 2008-10-02 2008 0 0 1 0 0 1 0 IA 1.0 3921.5 6.538042 -2.82373
It’s Perfectly Normal 1025 Harris, Robie 2005-04-01 2005 0 1 0 0 0 1 0 IN -6.2 1086.5 2.538042 -4.62373
book_challenge_df |>
  filter(state == 'PA') |>
  slice_sample(n = 10)
title book_id author date year removed explicit antifamily occult language lgbtq violent state political_value_index median_income hs_grad_rate college_grad_rate
Thirteen Reasons Why 1794 Asher, Jay 2009-11-09 2009 0 0 0 0 0 0 0 PA 2 4218 2.338042 -1.62373
Envy 636 Brown, Sandra 2009-11-09 2009 0 0 0 0 0 0 0 PA 2 4218 2.338042 -1.62373
Lucky 1199 Sebold, Alice 2009-11-09 2009 0 0 0 0 0 0 0 PA 2 4218 2.338042 -1.62373
Are You In the House Alone? 155 Peck, Richard 2009-11-09 2009 0 0 0 0 0 0 0 PA 2 4218 2.338042 -1.62373
Snowman, The 1659 Stine, R.L. 2008-06-07 2008 0 0 0 0 0 0 1 PA 2 4218 2.338042 -1.62373
Copper Sun 438 Draper, Sharon 2009-11-09 2009 0 0 0 0 0 0 0 PA 2 4218 2.338042 -1.62373
Twilight (series) 1861 Meyer, Stephenie 2009-11-09 2009 0 0 0 0 0 0 0 PA 2 4218 2.338042 -1.62373
A Separate Peach 45 Knowles, John 2009-11-09 2009 0 0 0 0 0 0 0 PA 2 4218 2.338042 -1.62373
Kissing Kate 1090 Myracle, Lauren 2009-11-09 2009 0 0 0 0 0 0 0 PA 2 4218 2.338042 -1.62373
Mama Fue Pequena Antes de Ser Mayor 1215 Larrodo, Valerie; Desmareau, Claudine (illustrator); Rubio, Esther (translator) 2007-05-23 2007 1 0 0 0 1 0 0 PA 2 4218 2.338042 -1.62373